package com.td.erhome.ui.home;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.Toast;

import com.td.erhome.R;
import com.td.erhome.base.BaseActivity;
import com.td.erhome.models.CityInfo;
import com.td.erhome.models.CityInfoLitePal;
import com.td.erhome.net.Api;
import com.td.erhome.net.BaseSubscriber;

import java.io.File;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class HomeActivity extends BaseActivity {

    @Bind(R.id.frame_layout)
    FrameLayout mFrameLayout;
    @Bind(R.id.ra_home)
    RadioButton mRaHome;
    @Bind(R.id.ra_msg)
    RadioButton mRaMsg;
    @Bind(R.id.ra_min)
    RadioButton mRaMin;

    private FragmentManager mFragmentManager;
    private HomeFragment mHomeFragment;
    private MsgFragment msgFragment;
    private MinFragment minFragment;
    private long exitTime = 0;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_home;
    }

    @Override
    protected void initView() {
        mFragmentManager = getSupportFragmentManager();
        addFragment("首页");
        copyDBFile();
    }

    @Override
    protected void releaseResource() {

    }

    /**
     * 添加Fragment
     */
    public void addFragment(String i) {
        //开启一个事务
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        //先全部隐藏
        hideFragment(transaction);
        switch (i) {
            case "首页":
                if (mHomeFragment == null) {
                    mHomeFragment = HomeFragment.newInstance();
                    transaction.add(R.id.frame_layout, mHomeFragment);
                } else {
                    transaction.show(mHomeFragment);
                }
                break;
            case "信息":
                if (msgFragment == null) {
                    msgFragment = MsgFragment.newInstance();
                    transaction.add(R.id.frame_layout, msgFragment);
                } else {
                    transaction.show(msgFragment);
                }

                break;
            case "我":
                if (minFragment == null) {
                    minFragment = MinFragment.newInstance();
                    transaction.add(R.id.frame_layout, minFragment);
                } else {
                    transaction.show(minFragment);
                }
                break;
        }
        //提交事务
        //  transaction.commit();
        transaction.commitAllowingStateLoss();
    }

    /**
     * 隐藏Fragment
     *
     * @param transaction
     */
    private void hideFragment(FragmentTransaction transaction) {
        if (mHomeFragment != null) {
            transaction.hide(mHomeFragment);
        }
        if (msgFragment != null) {
            transaction.hide(msgFragment);
        }
        if (minFragment != null) {
            transaction.hide(minFragment);
        }

    }


    @OnClick({R.id.ra_home, R.id.ra_msg, R.id.ra_min})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ra_home:
                addFragment("首页");
                break;
            case R.id.ra_msg:
                addFragment("信息");
                break;
            case R.id.ra_min:
                addFragment("我");
                break;
        }
    }

    public void getCityInfo(){
                Api.getRetrofit().getChengDuAreaInfo()
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new BaseSubscriber<CityInfo>(HomeActivity.this){
                            @Override
                            public void onStart() {
                            }
                            @Override
                            public void onNext(CityInfo cityInfoBaseBean) {
                                super.onNext(cityInfoBaseBean);

                                List<CityInfo.DataBean> data1 = cityInfoBaseBean.getData();
                                CityInfoLitePal citypal;

                                for (int i = 0; i <data1.size() ; i++) {
                                    citypal = new CityInfoLitePal();
                                    citypal.setArea_id(data1.get(i).getProvince_id());
                                    citypal.setArea_name(data1.get(i).getProvince_name());
                                    citypal.save();
                                    citypal = null;
                                    List<CityInfo.DataBean.CityListBean> city_list = data1.get(i).getCity_list();
                                    for (int j = 0; j <city_list.size() ; j++) {
                                        citypal = new CityInfoLitePal();
                                        citypal.setArea_id(city_list.get(j).getCity_id());
                                        citypal.setArea_name(city_list.get(j).getCity_name());
                                        citypal.save();
                                        citypal = null;
                                    }
                                }



                                /*List<CityInfo.DataBean> data = cityInfoBaseBean.data;
                                CityInfoLitePal citypal;
                                for (int i = data.size()-2; i >=0 ; i--) {
                                    citypal = new CityInfoLitePal();
                                    citypal.setArea_id(data.get(i).area_id*1000);
                                    citypal.setArea_name(data.get(i).area_name);
                                    citypal.save();
                                    citypal = null;
                                    List<CityInfo.DataBean.StreetlistBean> streetlist = data.get(i).streetlist;
                                    for (int j = 0; j <streetlist.size() ; j++) {
                                        citypal = new CityInfoLitePal();
                                        citypal.setArea_id(streetlist.get(j).area_ids);
                                        citypal.setArea_name(streetlist.get(j).area_name);
                                        citypal.save();
                                        citypal = null;
                                    }
                                }*/
                            }
                        });
            }

    public void copyDBFile(){
        String mLoadPath = getDatabasePath("cityinfo.db").getPath();
        File dbFile = new File(mLoadPath);
        boolean exists = dbFile.exists();
        if (!dbFile.exists()){
            getCityInfo();
        }
    }

    /**
     * 双击退出程序
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            if((System.currentTimeMillis()- exitTime) > 2000){
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
